﻿@page "/drawing/add/{ProjectId}"

@inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager
@inject IDrawingService DrawingService
@inject IProjectService ProjectService

<PageTitle>新增图纸</PageTitle>
@if (_projectDto != null)
{
    <h3>@_projectDto.OdpNumber-@_projectDto.Name</h3>
}

<EditForm Model="_drawingDtos" OnSubmit="Save">
    <button class="btn btn-primary mb-2" type="button" @onclick="Add">新增一条</button><br />
    @foreach (var dto in _drawingDtos)
    {
        <div class="row">
            <div class="col">
                <span>Item编号 ：</span><br />
                <div class="input-group mb-2">
                    <span class="input-group-text">Item编号</span>
                    <input class="form-control" placeholder="Item" @bind-value="@dto.ItemNumber" />
                </div>
            </div>
            <div class="col">
                <span>分批 ：</span><br />
                <div class="input-group mb-2">
                    <span class="input-group-text">分批</span>
                    <input class="form-control" type="number" @bind-value="@dto.Batch"/>
                </div>
            </div>
            <div class="col">
                <MultiFileUpload Title="图纸(可添加多张)" FileType="application/pdf" @bind-MultiFileUrl="@dto.DrawingUrl" />
            </div>
            
                <AuthorizeView Roles="admin">
                    <Authorized Context="auth">
                        <div class="col">
                        <FileUpload Title="截图(测试用)" @bind-FileUrl="@dto.ImageUrl"/>
                    </div>
                    </Authorized>
                </AuthorizeView>
            
            <div class="col">
                <span class="text-danger">移除此条 ：</span><br />
                <button class="btn" type="button" @onclick="@(() => _drawingDtos.Remove(dto))">
                    <span class="oi oi-trash text-danger"></span>
                </button>
            </div>
        </div>
    }

    <CompSaveCancelButtons CancelUrl="projects" />
</EditForm>
@code {
    [Parameter]
    public string ProjectId { get; set; }
    private ProjectDto _projectDto;
    private List<DrawingDto> _drawingDtos = new();

    protected override async Task OnInitializedAsync()
    {
        var response = await ProjectService.GetFirstOrDefaultAsync(Guid.Parse(ProjectId));
        if (!response.Status) NavigationManager.NavigateTo("projects");
        _projectDto = response.Result;
        Add();
    }
    private void Add()
    {
        _drawingDtos.Add(new DrawingDto { ProjectId = Guid.Parse(ProjectId) });
    }
    private async Task Save()
    {
        var isEmpty = _drawingDtos.Aggregate(false, (current, dto) => current || string.IsNullOrEmpty(dto.ItemNumber));
        if (isEmpty)
        {
            await JsRuntime.InvokeVoidAsync("alert", "填写的信息不完整");
            return;
        }

        var result = true;
        foreach (var dto in _drawingDtos)
        {
            //为了避免Item号前后有空格，导致后续创建文件夹时出现错误，此处消除空格
            dto.ItemNumber = dto.ItemNumber.Trim();
            result = result&&(await DrawingService.AddAsync(dto)).IsSuccessStatusCode;
        }
        if (result) NavigationManager.NavigateTo($"drawings/{ProjectId}");
    }




}
